<?php

/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class AdminController extends CController {
    
    public $menu = array();
    public $breadcrumbs = array();
    private $_pageTitle;
    private $_pageHeader;
    private $_pageToolbar;
    private $_mediaConfig = array();    
        
    public function __construct($id, $module = null) {
        parent::__construct($id, $module);
    }

    public function init() {
        parent::init();
        Yii::app()->setLanguage('vn');
	
	$user = app()->user;
	
	
	if (isset($user) && isset($user->id)) {
	    //Check "Does user id exist in database?"
	    $u = User::model()->findByPk($user->id);
	    
	    if ($u == null) {
		$user->logout();
	    }
	}
    }

    /**
     * Filter by using Modules Rights
     * 
     * @return type 
     */
    public function filters() {
        return array('rights',);
    }

    /**
     * List of allowd default Actions for the user
     * @return type 
     */
    public function allowedActions() {
        return '';
    }

    /**
     * The filter method for 'rights' access filter.
     * This filter is a wrapper of {@link CAccessControlFilter}.
     * @param CFilterChain $filterChain the filter chain that the filter is on.
     */
    public function filterRights($filterChain) {
        $filter = new RightsFilter;
        $filter->allowedActions = $this->allowedActions();
        $filter->filter($filterChain);
        
    }
    
    public function showMessage($key, $message){
         Yii::app()->user->setFlash($key, $message);
    }

    /**
     * Denies the access of the user.
     * @param string $message the message to display to the user.
     * This method may be invoked when access check fails.
     * @throws CHttpException when called unless login is required.
     */
    public function accessDenied($message = null) {
        if ($message === null)
            $message = t('MSG_NOT_PERMISSION','messages');

        $user = Yii::app()->getUser();
        
        if ($user->isGuest === true){
            if(Yii::app()->request->isAjaxRequest ){
                echo CJSON::encode(array(
                    'status' => 'render',
                    'content' => $message,
                    'expired' => url('authorization/login')
                    )
                );
            }
            else
                $user->loginRequired();
        }
            
        else
        {
            if(Yii::app()->request->isAjaxRequest)
            {
                echo CJSON::encode(array(
                    'status' => 'render',
                    'content' => $message)
                );
            }
            else
            {                
                throw new CHttpException(403, $message);
            }
        }
    }        
    
    /**
     * @return string the page title. Defaults to the controller name and the action name.
     */
    public function getPageTitle() {
        $removeAppName = ":R";
    	if ($this->_pageTitle !== null)
        {
        	$pos = strpos($this->_pageTitle, $removeAppName);
        	if($pos === false)        	
        		return $this->_pageTitle . ' - ' . Yii::app()->name;
        	else
        		return str_replace($removeAppName, "", $this->_pageTitle);        		
        }
        else {
            $name = ucfirst(basename($this->getId()));
            if ($this->getAction() !== null && strcasecmp($this->getAction()->getId(), $this->defaultAction))
                return $this->_pageTitle = ucfirst($this->getAction()->getId()) . ' - ' . Yii::app()->name;
            else
                return $this->_pageTitle = $name . ' - ' . Yii::app()->name;
        }
    }
    
    
    public function setPageTitle($value)
    {
        $this->_pageTitle = $value;
    }

    /**
     * @return string the page header.
     */
    public function getPageHeader() {
        return $this->_pageHeader;
    }

    /**
     * @param string $value the page header.
     */
    public function setPageHeader($value) {
        $this->_pageHeader = $value;
    }

    /**
     * @return string the toolbar.
     */
    public function getPageToolbar() {
        return $this->_pageToolbar;
    }

    /**
     * @param string $value the page toolbar.
     */
    public function setPageToolbar($value) {
        $this->_pageToolbar = $value;
    }
    
    /**
     * @return array media configuration.
     */    
    public function getMediaConfig(){
        return $this->_mediaConfig;
    }
    
    /**
     * load config of media item has name $value
     * @param type $value key of media item
     */
    public function setMediaConfig($value)
    {
        $config = Yii::app()->params['media'][$value];
        $arr = array();
        $arr["path"] = ROOT_FOLDER .$config["path"];
	$arr['media_folder'] = $config["path"];
        $arr["url"] = dirname(Yii::app()->baseUrl).$config["path"];      
        $arr["folder_permission"]["pattern"] = $config["folder_permission"]["pattern"];
        $arr["folder_permission"]["read"] = $config["folder_permission"]["read"];
        $arr["folder_permission"]["write"] = $config["folder_permission"]["write"];
        $arr["folder_permission"]["locked"] = $config["folder_permission"]["locked"];
        $arr["folder_permission"]["hidden"] = $config["folder_permission"]["hidden"];
        $arr["file_permission"]["pattern"] = $config["file_permission"]["pattern"];
        $arr["file_permission"]["hidden"] = $config["file_permission"]["hidden"];
        $this->_mediaConfig = $arr;
    }     
}